<template>
  <!-- 用户管理 -->
  <div class="users-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-edit" @click="handleAdd('editForm')">
          添加
        </el-button>
        <el-input v-model="search" placeholder="名字" style="width: 200px;padding-left: 20px" />
      </div>
      <el-table
        :data="userlist.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%"
        class="list-table"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="180"
        />
        <el-table-column
          prop="name"
          label="名字"
          width="180"
        />
        <el-table-column
          prop="userType"
          label="类型"
          width="180"
        />
        <el-table-column
          prop="userStatus"
          label="状态"
          width="180"
        />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleBan(scope.row.id)"
            >封禁</el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          layout="prev, pager, next"
          :total="totalpages"
          background
          @current-change="onCurrentChange"
        />
      </div>
    </el-card>

    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogForm">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="editForm.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="用户类型" :label-width="formLabelWidth">
          <el-radio-group v-model="editForm.userType">
            <el-radio label="0">0: 管理员</el-radio>
            <el-radio label="1">1: 企业用户</el-radio>
            <el-radio label="2">2: 供应商</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel('editForm')">取 消</el-button>
        <el-button type="primary" @click.native="handleUpdate('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getUsers,
  deleteUsers,
  banUsers,
  addUsers,
  resetUsers
} from '@/api/user'

export default {
  data() {
    return {
      userlist: [],
      search: '',
      dialogForm: false,
      editForm: {
        name: '',
        password: '',
        userType: 0
      },
      totalpages: 0,
      titleMap: {
        add: '新建用户',
        edit: '编辑用户'
      },
      dialogStatus: '',
      flag: 0
    }
  },
  created() {
    this.loadUserList(1)
    this.flag = 0
  },
  methods: {
    // 获取用户列表
    loadUserList(page) {
      // 获取用户列表
      getUsers({
        current: page,
        size: 10
      }).then(res => {
        console.log(res)
        this.userlist = res.data
        this.totalpages = res.data[0].pages * 10
      })
    },
    // 根据传来的pages确定最多页码
    onCurrentChange(page) {
      this.loadUserList(page)
    },
    // 删除用户
    handleDelete: function(id) {
      // 删除用户
      this.$confirm('确认删除?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行
        console.log(id)
        new Promise((resolve, reject) => {
          deleteUsers({ id: id }).then(res => {
          // 删除成功
            this.loadUserList(this.page)
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 封禁用户
    handleBan: function(id) {
      // 封禁用户
      this.$confirm('确认封禁?', '封禁提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行
        console.log(id)
        new Promise((resolve, reject) => {
          banUsers({ id: id }).then(res => {
          // 封禁成功
            this.loadUserList(this.page)
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消封禁'
        })
      })
    },
    // 添加用户
    handleAdd: function(editForm) {
      this.dialogForm = true
      this.dialogStatus = 'add'
      this.flag = 1
      this.editForm.name = editForm.name
      this.editForm.password = editForm.password
      this.editForm.userType = editForm.userType
      // this.dialogForm.push({
      //   id: this.id,
      //   Name: this.name
      // })
    },
    // 编辑用户
    handleEdit: function(index, row) {
      console.log(row)
      this.dialogForm = true
      this.editForm = Object.assign({}, row)
      this.dialogStatus = 'edit'
      this.flag = 0
    },
    // 更新信息
    handleUpdate(forName) {
      if (this.flag === 1) {
        addUsers(this.editForm).then(res => {
          console.log(this.editForm)
          console.log(res)
          this.loadUserList()
        })
        this.$message({
          type: 'success',
          message: '创建成功'
        })
      } else if (this.flag === 0) {
        resetUsers(this.editForm).then(res => {
          console.log(res)
          this.loadUserList()
        })
        this.$message({
          type: 'success',
          message: '修改成功'
        })
      }
      this.dialogForm = false
    },
    // 取消修改
    handleCancel(formName) {
      this.$message({
        type: 'info',
        message: '已取消'
      })
      this.dialogForm = false
    }
  }
}
</script>

<style scoped>

.box-card {
  height: 850px;
}

.list-table {
  margin-bottom: 30px;
}

.el-table-column {
  align-content: center;
}
</style>
